package com.globo.devsup.client.view;

import com.globo.devsup.client.presenter.Presenter;
import com.gwtext.client.core.Connection;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.event.PanelListener;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;
import com.gwtext.client.widgets.tree.AsyncTreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.XMLTreeLoader;

public class PrincipalView extends PanelView {

	/**
	 * ############### COMPONENTS ############### 
	 */
	private final Panel principalPanel 	= new Panel			();
	private final Panel westPanel 		= new Panel			();
	private final Panel centerPanel 	= new Panel			();
	private final Panel southPanel 		= new Panel			();
	private final TreePanel navPanel 	= new TreePanel		();
	
	private final Panel logoPanel 		= new Panel			();
	private final ToolTip westPanelTip 	= new ToolTip		("Barra de navegação");
	private final ToolTip consoleTip 	= new ToolTip		("Barra de console");
	private final XMLTreeLoader loader 	= new XMLTreeLoader	();
	private final AsyncTreeNode root 	= new AsyncTreeNode	("Menu", loader);
	
	private final BorderLayoutData westData = 	new BorderLayoutData(RegionPosition.WEST);
	private final BorderLayoutData centerData = new BorderLayoutData(RegionPosition.CENTER);
	private final BorderLayoutData southData = 	new BorderLayoutData(RegionPosition.SOUTH);
	
	public Panel getPrincipalPanel() {
		return principalPanel;
	}
	public Panel getWestPanel() {
		return westPanel;
	}
	public Panel getCenterPanel() {
		return centerPanel;
	}
	public Panel getSouthPanel() {
		return southPanel;
	}
	public TreePanel getNavPanel() {
		return navPanel;
	}
	public Panel getLogoPanel() {
		return logoPanel;
	}
	public ToolTip getWestPanelTip() {
		return westPanelTip;
	}
	public ToolTip getConsoleTip() {
		return consoleTip;
	}
	public XMLTreeLoader getLoader() {
		return loader;
	}
	public AsyncTreeNode getRoot() {
		return root;
	}
	public BorderLayoutData getWestData() {
		return westData;
	}
	public BorderLayoutData getCenterData() {
		return centerData;
	}
	public BorderLayoutData getSouthData() {
		return southData;
	}

	/**
	 * ############### SETTINGS ############### 
	 */
    public View getID(){
        return View.PRINCIPAL_PANEL;
    }
    
    public PrincipalView(Presenter presenter) {
		super(presenter);
    	
		setLayout(new BorderLayout());
		setLayoutData();
		
		westPanel.setTitle("Navegação");
		westPanel.setWidth(220);  
		westPanel.setCollapsible(true);
		westPanelTip.applyTo(westPanel);
		
		southPanel.setTitle("Console");
		southPanel.setHeight(100);  
		southPanel.setCollapsible(true);
				
		logoPanel.setBorder(false);
		logoPanel.setWidth(215);
		logoPanel.setHeight(90);
		//logoPanel.setBodyStyle("background:transparent url(com.globo.devsup.Principal/js/ext/resources/images/logotipo_213x91.png) repeat-x 0 -1px;");

		loader.setDataUrl("http://www.gwt-ext.com/demo/data/countries.xml");  
		loader.setMethod(Connection.GET);  
		loader.setRootTag("countries");  
		loader.setFolderTitleMapping("@title");  
		loader.setFolderTag("team");  
		loader.setLeafTitleMapping("@title");  
		loader.setLeafTag("country");  
		loader.setQtipMapping("@qtip");  
		loader.setDisabledMapping("@disabled");  
		loader.setCheckedMapping("@checked");  
		loader.setIconMapping("@icon");  
		loader.setAttributeMappings(new String[]{"@rank"});
		
		navPanel.setAnimate(true);  
		navPanel.setEnableDD(true);  
		navPanel.setContainerScroll(true);  
		navPanel.setRootVisible(true);
		navPanel.setRootNode(root);  
		navPanel.expandAll();
		root.expand();
		
		westPanel.add(logoPanel, new BorderLayoutData(RegionPosition.NORTH));
		westPanel.add(navPanel, new BorderLayoutData(RegionPosition.CENTER));
		
		add(westPanel, westData);
		add(centerPanel, centerData);
		add(southPanel, southData);
    }
    
    private void setLayoutData() {
    	westData.setSplit(true);
		westData.setMinSize(220);  
		westData.setMaxSize(300);
		westData.setMargins(3, 3, 0, 0);  
		westData.setCMargins(3, 3, 3, 0);
		  
		centerData.setMargins(3, 0, 3, 0);
		centerData.setCMargins(3, 3, 3, 3);
		
		southData.setSplit(true);
		southData.setMinSize(100);  
		southData.setMaxSize(300);	
		southData.setMargins(0, 3, 3, 3);  
		southData.setCMargins(3, 3, 3, 3);
    }
    
    
	/**
	 * ############### COMPONENTS LISTENERS ############### 
	 */
    
    public void setPanelActionListener(PanelListener listener){
    	addListener(listener);
    }
}
